package com.ct.ecommerce.idgen.controller;

import com.ct.ecommerce.api.idgen.client.IdGeneratorService;
import com.ct.ecommerce.api.idgen.client.SnGeneratorService;
import com.ct.ecommerce.framework.core.response.R;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author changtong
 * @since 2021/5/19
 */
@RestController
@RequestMapping("/idGen")
@Slf4j
public class IdgenController {

    @Resource
    private IdGeneratorService idGeneratorService;

    @Resource
    private SnGeneratorService snGeneratorService;

    @GetMapping("/")
    public R<Long> get(){
        try {

            return R.ok(idGeneratorService.get());
        } catch (Exception e) {
            log.error("获取id出错 : ",e);
            return R.fail("获取id出错...");
        }

    }

    @GetMapping("/sn/{serialNumberType}")
    public R<String> getSn(@PathVariable("serialNumberType")Integer serialNumberType){
        try {
            return R.ok(snGeneratorService.get(serialNumberType));
        } catch (Exception e) {
            log.error("获取sn出错 : ",e);
            return R.fail("获取sn出错...");
        }

    }



}
